const {
  getGetOrdersList,
  postOrdersAdd,
  applyOrders,
  deleteOrders,
  getOrdersId,
  getOrdersSalesIdList,
  getOrdersOne
} = require('../dao/modules/ordersDao')

const {
  salesModel
} = require('../dao/models/salesModels')

module.exports.getGetOrdersList = async (params) => {
  const data = await getGetOrdersList(params)
  if (data) {
    return {
      data,
      message: '查询成功',
      code: 1
    }
  }
  return {
    data,
    message: '查询失败',
    code: 0
  }

}



module.exports.postOrdersAdd = async (params) => {
  const data = await postOrdersAdd(params)
  if (data) {
    return {
      data,
      message: '增加成功',
      code: 1
    }
  }
  return {
    data,
    message: '增加失败',
    code: 0
  }

}



module.exports.applyOrders = async (params) => {
  const data = await applyOrders(params)
  if (data) {
    return {
      data,
      message: '退保成功',
      code: 1
    }
  }
  return {
    data,
    message: '退保失败',
    code: 0
  }

}
module.exports.deleteOrders = async (params) => {
  const data = await deleteOrders(params)
  if (data) {
    return {
      data,
      message: '删除成功',
      code: 1
    }
  }
  return {
    data,
    message: '删除失败',
    code: 0
  }

}

module.exports.getOrdersId = async (params) => {
  const data = await getOrdersId(params)
  if (data) {
    return {
      data,
      message: '查询成功',
      code: 1
    }
  }
  return {
    data,
    message: '查询失败',
    code: 0
  }

}

module.exports.getOrdersSalesIdList = async (params) => {
  const {
    salesId
  } = params
  const salesIdData = await salesModel.find({
    name: {
      $regex: salesId,
      $options: '$i'
    },
  }, '_id');

  const data = await getOrdersSalesIdList(salesIdData)

  if (data) {
    return {
      data,
      message: '查询成功',
      code: 1
    }
  }
  return {
    data,
    message: '查询失败',
    code: 0
  }
        
    }


    //获取单个订单信息
    module.exports.getOrdersOne = async  (params) =>{
      const data=  await getOrdersOne(params)
      return data
          
      }
